<template>
  <addEditBaseDepartmentView ref="addEditBaseDepartmentView" />
  <addEditBaseDepartmentManagerView ref="addEditBaseDepartmentManagerView" />
  <addEditBaseDepartmentClassInfoView ref="addEditBaseDepartmentClassInfoView" />
  <baseTeacherDepartmentList ref="baseTeacherDepartmentList" />
  <baseTeacherDepartmentListCopy ref="baseTeacherDepartmentListCopy" />
  <addFileView ref="addFileView" />
  <el-main>
    <div class="teacher">
      <div class="app-table">
        <div class="app-body">
          <div class="app-search">
            <el-form :inline="true" class="demo-form-inline" v-model="form">
              <el-form-item :label="name">
                <el-input :placeholder="placeholder" v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button class="serach_btn" plain type="primary" @click="onSubmit"
                  @keyup.enter="searchEnter">搜索</el-button>
                <el-button class="add_btn" plain type="success" @click="add">添加</el-button>
                <!-- <el-button type="warning" @click="exportExcel"
                  >导出报表</el-button
                >
                <el-button type="danger" @click="importExcel"
                  >批量导入<el-icon class="el-icon--right"><Upload /></el-icon
                ></el-button> -->
              </el-form-item>
            </el-form>
          </div>
          <el-table size="default" :data="list" style="width: 100%" :row-class-name="tableRowClassName">
            <el-table-column type="index" width="60" label="序号" />
            <el-table-column prop="name" :label="name" />
            <el-table-column prop="orderNum" label="权重" />
            <el-table-column prop="leader1Name" v-if="type == 2" label="年级主任" />
            <el-table-column prop="leader2Name" label="中心主任" v-if="type != 3" />
            <el-table-column prop="leader3Name" label="主管校长" v-if="type != 3" />
            <el-table-column prop="leader4Name" label="校区校长" v-if="type != 3" />
            <el-table-column label="操作" width="380px">
              <template style="display: block" #default="scope">
                <el-button class='edit_btn1' size="small" type="primary" v-if="type != 3"
                  @click="baseTeacherDepartmentList(scope.row)">人员列表
                </el-button>
                <!-- 学科的人员列表 -->
                <el-button class='edit_btn1' size="small" type="primary" v-if="type == 3"
                  @click="baseTeacherDepartmentList1(scope.row)">人员列表
                </el-button>
                <el-button class='edit_btn1' size="small" type="warning"
                  @click="editBaseDepartmentManager(scope.row)">主管人员
                </el-button>
                <el-button class='edit_btn1' size="small" type="error" v-if="type == 2"
                  @click="baseTeacherDepartmentClassInfo(scope.row)">班级设置
                </el-button>
                <el-button class="edit_btn" size="small" type="success" @click="editBaseDepartment(scope.row)">编辑
                </el-button>
                <el-button class="del_btn" size="small" type="danger" @click="deleteBaseDepartment(scope.row.id)">删除
                </el-button>
              </template>
            </el-table-column>
            <template v-slot:empty>
              <el-empty description="暂无数据"></el-empty>
            </template>
          </el-table>
          <el-config-provider :locale="locale">
            <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
              @current-change="changeCurrentPage">
            </el-pagination>
          </el-config-provider>
        </div>
        <bottomView></bottomView>
      </div>
    </div>
  </el-main>
</template>
<script>
import addEditBaseDepartmentView from "@/components/baseInfo/addEditBaseDepartment.vue";
import addEditBaseDepartmentManagerView from "@/components/baseInfo/addEditBaseDepartmentManager.vue";
import addEditBaseDepartmentClassInfoView from "@/components/baseInfo/addEditBaseDepartmentClassInfo.vue";
import baseTeacherDepartmentList from "@/components/baseInfo/baseTeacherDepartmentList.vue";
import baseTeacherDepartmentListCopy from "@/components/baseInfo/baseTeacherDepartmentListCopy.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
  name: "BaseDepartmentListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      list: [],
      name: "",
      placeholder: "",
      form: {
        leader4: "",
        leader3: "",
        leader2: "",
        leader1: "",
        name: "",
      },
    };
  },
  components: {
    addEditBaseDepartmentView, bottomView, addEditBaseDepartmentManagerView, baseTeacherDepartmentList, addEditBaseDepartmentClassInfoView,
    addFileView, baseTeacherDepartmentListCopy,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.changeType();
    this.getBaseDepartmentList();
  },
  watch: {
    $route() {
      this.changeType();
      this.getBaseDepartmentList();
    },
  },
  methods: {
    changeType() {
      this.type = this.$route.query.type;
      if (this.type == 1) {
        this.name = "处室名称";
        this.placeholder = "请输入处室名称";
      } else if (this.type == 2) {
        this.name = "级部名称";
        this.placeholder = "请输入级部名称";
      } else {
        this.name = "学科名称";
        this.placeholder = "请输入学科名称";
      }
    },

    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseDepartmentList();
      }
    },

    add() {
      this.$refs.addEditBaseDepartmentView.title = "添加信息";
      this.$refs.addEditBaseDepartmentView.name = this.name;
      this.$refs.addEditBaseDepartmentView.addBaseDepartmentForm.type = this.type;
      this.$refs.addEditBaseDepartmentView.placeholder = this.placeholder;
      this.$refs.addEditBaseDepartmentView.personnelType = this.type;
      this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
    },

    editBaseDepartment(obj) {
      this.$refs.addEditBaseDepartmentView.title = "修改信息";
      this.$refs.addEditBaseDepartmentView.name = this.name;
      this.$refs.addEditBaseDepartmentView.addBaseDepartmentForm.type = this.type;
      this.$refs.addEditBaseDepartmentView.placeholder = this.placeholder;
      this.$refs.addEditBaseDepartmentView.getBaseDepartmentById(obj.id);
      this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
      this.$refs.addEditBaseDepartmentView.getBaseDepartmentList()
    },

    editBaseDepartmentManager(obj) {
      this.$refs.addEditBaseDepartmentManagerView.title = "主管人员";
      this.$refs.addEditBaseDepartmentManagerView.name = this.name;
      this.$refs.addEditBaseDepartmentManagerView.type = this.type;
      this.$refs.addEditBaseDepartmentManagerView.getBaseTeacherListApp();
      this.$refs.addEditBaseDepartmentManagerView.placeholder = this.placeholder;
      this.$refs.addEditBaseDepartmentManagerView.getBaseDepartmentById(obj.id);
      this.$refs.addEditBaseDepartmentManagerView.dialogFormVisible = true;
    },

    baseTeacherDepartmentClassInfo(obj) {
      this.$refs.addEditBaseDepartmentClassInfoView.title = obj.name + "-班级设置";
      this.$refs.addEditBaseDepartmentClassInfoView.name = obj.name;
      this.$refs.addEditBaseDepartmentClassInfoView.getAllClassByDepartmentId(obj.id);
      this.$refs.addEditBaseDepartmentClassInfoView.getBaseClassInfoList(obj.gradeId);
      this.$refs.addEditBaseDepartmentClassInfoView.departmentId = obj.id;
      this.$refs.addEditBaseDepartmentClassInfoView.dialogFormVisible = true;
    },

    baseTeacherDepartmentList(obj) {
      this.$refs.baseTeacherDepartmentList.title = obj.name;
      this.$refs.baseTeacherDepartmentList.getBaseTeacherDepartmentList(obj.id);
      this.$refs.baseTeacherDepartmentList.dialogFormVisible = true;
      this.$refs.baseTeacherDepartmentList.type = this.type;
    },

    baseTeacherDepartmentList1(obj) {
      this.$refs.baseTeacherDepartmentListCopy.title = obj.name;
      this.$refs.baseTeacherDepartmentListCopy.getBaseTeacherDepartmentList(obj.id);
      this.$refs.baseTeacherDepartmentListCopy.dialogFormVisible = true;
      this.$refs.baseTeacherDepartmentListCopy.type = this.type;
    },

    deleteBaseDepartment(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseDepartment/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseDepartmentList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },

    reloadData() {
      this.page = 1;
      this.getBaseDepartmentList();
    },

    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入年级组、处室表";
      this.$refs.addFileView.uploadApiUrl =
        "/javaApi/baseDepartment/importExcel";
    },

    exportExcel() {
      httpService(
        "/javaApi/baseDepartment/exportExcel",
        {
          leader4: this.form.leader4,
          leader3: this.form.leader3,
          leader2: this.form.leader2,
          leader1: this.form.leader1,
          name: this.form.name,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },

    onSubmit() {
      this.getBaseDepartmentList();
    },

    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getBaseDepartmentList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseDepartmentList();
    },

    tableRowClassName({ row, rowIndex }) {
      return "";
    },

    getBaseDepartmentList() {
      httpService(
        "/javaApi/baseDepartment/list/" + this.page + "/" + this.pageSize,
        {
          leader4: this.form.leader4,
          leader3: this.form.leader3,
          leader2: this.form.leader2,
          leader1: this.form.leader1,
          name: this.form.name,
          type: this.type,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },

  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 5px 10px 10px;
}

.app-search {}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 204px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.export_btn {
  background-color: #FF7200;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.batch_del_btn {
  background-color: #FF0000;
  border-radius: 8px;
  color: #fff;
  border: none;
}

.edit_btn1 {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

.edit_btn {
  background-color: #fff;
  color: #00c78c;
  border: none;
}


.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}
</style>
